package com.ht.oss.utils;

import cn.hutool.core.thread.ExecutorBuilder;
import com.ht.oss.config.GlobalConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

@Component
public class GlobalThreadPool implements ExecutorService {
    private ExecutorService EX;

    @Autowired
    public void GlobalConfig(GlobalConfig globalConfig) {
        this.EX = ExecutorBuilder
                .create()
                .setCorePoolSize(globalConfig.getGlobalThreadPoolCoreSize())
                .setMaxPoolSize(globalConfig.getGlobalThreadPoolMaxSize())
                .setWorkQueue(new LinkedBlockingQueue<>(100))
                .build();
    }

    @Override
    public void shutdown() {
        EX.shutdown();
    }

    @Override
    public List<Runnable> shutdownNow() {
        return EX.shutdownNow();
    }

    @Override
    public boolean isShutdown() {
        return EX.isShutdown();
    }

    @Override
    public boolean isTerminated() {
        return EX.isTerminated();
    }

    @Override
    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        return EX.awaitTermination(timeout, unit);
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        return EX.submit(task);
    }

    @Override
    public <T> Future<T> submit(Runnable task, T result) {
        return EX.submit(task, result);
    }

    @Override
    public Future<?> submit(Runnable task) {
        return EX.submit(task);
    }

    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
        return EX.invokeAll(tasks);
    }

    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
        return EX.invokeAll(tasks, timeout, unit);
    }

    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
        return EX.invokeAny(tasks);
    }

    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return EX.invokeAny(tasks, timeout, unit);
    }

    @Override
    public void execute(Runnable command) {
        EX.execute(command);
    }
}
